﻿
namespace VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;

    using Microsoft.TeamFoundation.TestManagement.Client;
    using VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls.DataContracts;

    public class SelectTestSuitesViewModel
    {
        private readonly IEnumerable<ITestSuiteBase> tfsTestSuites;
        private List<int> testSuiteIdList;

        public SelectTestSuitesViewModel(IEnumerable<ITestSuiteBase> testSuites, List<int> testSuiteIdList)
        {
            this.tfsTestSuites = testSuites;
            this.testSuiteIdList = testSuiteIdList;
            try
            {
                this.FillTestSuitesList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error initializing select test suites!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public List<TestSuite> VmTestSuites { get; set; }

        private void FillTestSuitesList()
        {
            this.VmTestSuites = new List<TestSuite>();
            if (this.testSuiteIdList == null)
            {
                this.testSuiteIdList = new List<int>();
            }
            if (this.testSuiteIdList == null)
            {
                return;
            }

            foreach (var tfsTestSuite in this.tfsTestSuites)
            {
                if (tfsTestSuite.Parent != null)
                {
                    continue;
                }
                var vmTestSuite = new TestSuite() { SuiteTitle = tfsTestSuite.Title, SuiteId = tfsTestSuite.Id, TestSuites = new List<TestSuite>(), IsChecked = true };
                this.VmTestSuites.Add(vmTestSuite);
                this.FillTestSuiteListRecursive(vmTestSuite, tfsTestSuite);
            }
        }

        private void FillTestSuiteListRecursive(TestSuite vmtestSuite, ITestSuiteBase testsuite)
        {
            if (testsuite == null)
            {
                return;
            }
            var staticTestSuite = (IStaticTestSuite)testsuite;
            vmtestSuite.TestSuites = new List<TestSuite>();
            foreach (var subSuite in staticTestSuite.SubSuites)
            {
                var isChecked = this.testSuiteIdList.Contains(subSuite.Id);
                var vmSubSuite = new TestSuite() { SuiteTitle = subSuite.Title, SuiteId = subSuite.Id, IsChecked = isChecked };
                vmtestSuite.TestSuites.Add(vmSubSuite);
                var testSuiteFromId = this.GetTestSuiteById(subSuite.Id);
                if (testSuiteFromId.TestSuiteType == TestSuiteType.StaticTestSuite)
                {
                    this.FillTestSuiteListRecursive(vmSubSuite, testSuiteFromId);
                }
            }
        }

        private ITestSuiteBase GetTestSuiteById(int id)
        {
            return this.tfsTestSuites.FirstOrDefault(tfsTestSuite => tfsTestSuite.Id == id);
        }
    }
}
